<?php
/**
 * ALTER TABLE `products` ADD `technical` TEXT NOT NULL AFTER `detail` ,
 * ADD `attachment` TEXT NOT NULL AFTER `technical` 
 *
 */

class Product_model extends Model {
	
	private $table 	= 'products';
	private $table2	= 'products_field';
	
	function Product_model() {
		parent::Model ();
	}
		
	function update_product_field() {
		$data = array(	
			/*
			'field_name_1'	=> isset( $_POST['field_name_1'] ) ? htmlspecialchars( $_POST['field_name_1'] ) : '',
			'field_name_2'	=> isset( $_POST['field_name_2'] ) ? htmlspecialchars( $_POST['field_name_2'] ) : '',
			'field_name_3'	=> isset( $_POST['field_name_3'] ) ? htmlspecialchars( $_POST['field_name_3'] ) : '',
			'field_name_4'	=> isset( $_POST['field_name_4'] ) ? htmlspecialchars( $_POST['field_name_4'] ) : '',
			'field_name_5'	=> isset( $_POST['field_name_5'] ) ? htmlspecialchars( $_POST['field_name_5'] ) : '',
			*/
			'field_name_6'	=> isset( $_POST['field_name_6'] ) ? htmlspecialchars( $_POST['field_name_6'] ) : '',
			'field_name_7'	=> isset( $_POST['field_name_7'] ) ? htmlspecialchars( $_POST['field_name_7'] ) : '',
			'field_name_8'	=> isset( $_POST['field_name_8'] ) ? htmlspecialchars( $_POST['field_name_8'] ) : '',
			'field_name_9'	=> isset( $_POST['field_name_9'] ) ? htmlspecialchars( $_POST['field_name_9'] ) : '',
			'field_name_10'	=> isset( $_POST['field_name_10'] ) ? htmlspecialchars( $_POST['field_name_10'] ) : '',
			'field_name_11'	=> isset( $_POST['field_name_11'] ) ? htmlspecialchars( $_POST['field_name_11'] ) : '',
			'field_name_12'	=> isset( $_POST['field_name_12'] ) ? htmlspecialchars( $_POST['field_name_12'] ) : '',
			'field_name_13'	=> isset( $_POST['field_name_13'] ) ? htmlspecialchars( $_POST['field_name_13'] ) : '',
			'field_name_14'	=> isset( $_POST['field_name_14'] ) ? htmlspecialchars( $_POST['field_name_14'] ) : '',
			'field_name_15'	=> isset( $_POST['field_name_15'] ) ? htmlspecialchars( $_POST['field_name_15'] ) : '',
		);
			
		$this->db->where('id_lang', get_admin_lang() );		
		$this->db->update( $this->table2, $data);
	}
	
	function write_product() {
		$data = array(
			'id_lang'		=> get_admin_lang(),
			'cat_id'		=> isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '',
			'detail'		=> isset( $_POST['detail'] ) ? $_POST['detail'] : '' , 
			'image'			=> isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '', 
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',		
			'tags'			=> isset( $_POST['tags'] ) ? htmlspecialchars( $_POST['tags'] ) : '',		
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'new'			=> isset( $_POST['new'] ) ? (int)$_POST['new'] : 0, 
			'focus'			=> isset( $_POST['focus'] ) ? (int)$_POST['focus'] : 0,			
			'home'			=> isset( $_POST['home'] ) ? (int)$_POST['home'] : 0,			
			'orders'		=> 0,
			'date'			=> time(),
			'vnd'			=> isset( $_POST['vnd'] ) ? (int)$_POST['vnd'] : '',	
			'usd'			=> isset( $_POST['usd'] ) ? (int)$_POST['usd'] : '',					
			'seo_description'	=> isset( $_POST['seo_description'] ) ? htmlspecialchars( $_POST['seo_description'] ) : '',			
			'seo_keywords'		=> isset( $_POST['seo_keywords'] ) ? htmlspecialchars( $_POST['seo_keywords'] ) : '',							
			'technical'		=> isset( $_POST['technical'] ) ? $_POST['technical'] : '',					
			'attachment'	=> isset( $_POST['attachment'] ) ? $_POST['attachment'] : '',					
			'field_1'		=> isset( $_POST['field_1'] ) ? htmlspecialchars( $_POST['field_1'] ) : '',	
			'field_2'		=> isset( $_POST['field_2'] ) ? htmlspecialchars( $_POST['field_2'] ) : '',	
			'field_3'		=> isset( $_POST['field_3'] ) ? htmlspecialchars( $_POST['field_3'] ) : '',	
			'field_4'		=> isset( $_POST['field_4'] ) ? htmlspecialchars( $_POST['field_4'] ) : '',	
			'field_5'		=> isset( $_POST['field_5'] ) ? htmlspecialchars( $_POST['field_5'] ) : '',	
			'field_6'		=> isset( $_POST['field_6'] ) ? htmlspecialchars( $_POST['field_6'] ) : '',	
			'field_7'		=> isset( $_POST['field_7'] ) ? htmlspecialchars( $_POST['field_7'] ) : '',	
			'field_8'		=> isset( $_POST['field_8'] ) ? htmlspecialchars( $_POST['field_8'] ) : '',	
			'field_9'		=> isset( $_POST['field_9'] ) ? htmlspecialchars( $_POST['field_9'] ) : '',	
			'field_10'		=> isset( $_POST['field_10'] ) ? htmlspecialchars( $_POST['field_10'] ) : '',
			'field_11'		=> isset( $_POST['field_11'] ) ? htmlspecialchars( $_POST['field_11'] ) : '',
			'field_12'		=> isset( $_POST['field_12'] ) ? htmlspecialchars( $_POST['field_12'] ) : '',
			'field_13'		=> isset( $_POST['field_13'] ) ? htmlspecialchars( $_POST['field_13'] ) : '',
			'field_14'		=> isset( $_POST['field_14'] ) ? htmlspecialchars( $_POST['field_14'] ) : '',
			'field_15'		=> isset( $_POST['field_15'] ) ? htmlspecialchars( $_POST['field_15'] ) : ''
		);
		
		$this->db->insert( $this->table , $data );		
	}
	
	function list_product( $cat = 0, $num , $offset, &$total ) {
		$data 	= array ();
		$total 	= 0;

		if ( $cat == 0 )
			$where = '';
		else 
			$where = '`cat_id` = '.$cat.' AND ';
		
		$query = $this->db->query("SELECT `id`, `cat_id`, `name`, `description`, `detail`, `image`, `image_text`, `tags`, `home`, `focus`, `new`, `status`, `orders` FROM $this->table WHERE $where `id_lang` = '".get_admin_lang()."' ORDER BY `orders` ASC, `id` DESC LIMIT $offset, $num");
		
		if ( $query->num_rows () > 0 ) 
			$data = $query->result_array();
		
		
		// Tông số bản ghi
		$query = $this->db->query("SELECT `id` FROM $this->table WHERE $where `id_lang` = '".get_admin_lang()."' ");
		$total = $query->num_rows();
		
		$query->free_result ();
		return $data;
	}
	
	/**
	 * Chi tiết
	 */
	function select_product( $id = 0, $k = 0 ) {
		$data = array ();
		
		if ( is_numeric( $id ) ) {
			
			if ( $k == 0 ) {
				$sql = "SELECT a.`attachment` ,a.`technical`, a.`seo_keywords`, a.`seo_description`, a.`vnd`, a.`usd`, a.`id`, a.`cat_id`, a.`name`, a.`description`, a.`detail`, a.`image`, a.`image_text`, a.`tags`, a.`home`, a.`focus`, a.`new`, a.`status`, a.`orders`, a.`field_1`, a.`field_2`, a.`field_3`, a.`field_4`, a.`field_5`, a.`field_6`, a.`field_7`, a.`field_8`, a.`field_9`, a.`field_10`, a.`field_11`, a.`field_12`, a.`field_13`, a.`field_14`, a.`field_15`, b.`field_name_1`, b.`field_name_2`, b.`field_name_3`, b.`field_name_4`, b.`field_name_5`, b.`field_name_6`, b.`field_name_7`, b.`field_name_8`, b.`field_name_9`, b.`field_name_10`, b.`field_name_11`, b.`field_name_12`, b.`field_name_13`, b.`field_name_14`, b.`field_name_15` 
				FROM `$this->table` AS a LEFT JOIN `$this->table2` AS b ON a.`id_lang` = b.`id_lang`
				WHERE a.`id` = '$id' AND a.`id_lang` = '".get_admin_lang()."' 
				LIMIT 1 ";
			} else
				$sql = "SELECT `field_name_1`, `field_name_2`, `field_name_3`, `field_name_4`, `field_name_5`, `field_name_6`, `field_name_7`, `field_name_8`, `field_name_9`, `field_name_10`, `field_name_11`, `field_name_12`, `field_name_13`, `field_name_14`, `field_name_15` FROM `$this->table2` WHERE `id_lang` = '".get_admin_lang()."' LIMIT 1 ";
				
			$query = $this->db->query($sql);
			
			if ( $query->num_rows () > 0 ) 
				$data = $query->row_array();
			
			$query->free_result ();
		}
		return $data;
	}
	
	function update_product() {
		$data = array(
			'cat_id'		=> isset( $_POST['cat'] ) ? (int)$_POST['cat'] : 0,
			'name'			=> isset( $_POST['name'] ) ? htmlspecialchars( $_POST['name'] ) : '',
			'description'	=> isset( $_POST['description'] ) ? $_POST['description'] : '' ,
			'detail'		=> isset( $_POST['detail'] ) ? $_POST['detail'] : '' , 				
			'image'			=> isset( $_POST['pic'] ) ? htmlspecialchars( $_POST['pic'] ) : '', 
			'image_text'	=> isset( $_POST['picdesc'] ) ? htmlspecialchars( $_POST['picdesc'] ) : '',		
			'tags'			=> isset( $_POST['tags'] ) ? htmlspecialchars( $_POST['tags'] ) : '',
			'status'		=> isset( $_POST['status'] ) ? (int)$_POST['status'] : 0,
			'new'			=> isset( $_POST['new'] ) ? (int)$_POST['new'] : 0,
			'focus'			=> isset( $_POST['focus'] ) ? (int)$_POST['focus'] : 0,			
			'home'			=> isset( $_POST['home'] ) ? (int)$_POST['home'] : 0,
			'vnd'			=> isset( $_POST['vnd'] ) ? (int)$_POST['vnd'] : '',	
			'usd'			=> isset( $_POST['usd'] ) ? (int)$_POST['usd'] : '',	
			'seo_description'	=> isset( $_POST['seo_description'] ) ? htmlspecialchars( $_POST['seo_description'] ) : '',			
			'seo_keywords'		=> isset( $_POST['seo_keywords'] ) ? htmlspecialchars( $_POST['seo_keywords'] ) : '',	
			'technical'		=> isset( $_POST['technical'] ) ? $_POST['technical'] : '',					
			'attachment'	=> isset( $_POST['attachment'] ) ? $_POST['attachment'] : '',	
			'field_1'		=> isset( $_POST['field_1'] ) ? htmlspecialchars( $_POST['field_1'] ) : '',	
			'field_2'		=> isset( $_POST['field_2'] ) ? htmlspecialchars( $_POST['field_2'] ) : '',	
			'field_3'		=> isset( $_POST['field_3'] ) ? htmlspecialchars( $_POST['field_3'] ) : '',	
			'field_4'		=> isset( $_POST['field_4'] ) ? htmlspecialchars( $_POST['field_4'] ) : '',	
			'field_5'		=> isset( $_POST['field_5'] ) ? htmlspecialchars( $_POST['field_5'] ) : '',	
			'field_6'		=> isset( $_POST['field_6'] ) ? htmlspecialchars( $_POST['field_6'] ) : '',	
			'field_7'		=> isset( $_POST['field_7'] ) ? htmlspecialchars( $_POST['field_7'] ) : '',	
			'field_8'		=> isset( $_POST['field_8'] ) ? htmlspecialchars( $_POST['field_8'] ) : '',	
			'field_9'		=> isset( $_POST['field_9'] ) ? htmlspecialchars( $_POST['field_9'] ) : '',	
			'field_10'		=> isset( $_POST['field_10'] ) ? htmlspecialchars( $_POST['field_10'] ) : '',
			'field_11'		=> isset( $_POST['field_11'] ) ? htmlspecialchars( $_POST['field_11'] ) : '',
			'field_12'		=> isset( $_POST['field_12'] ) ? htmlspecialchars( $_POST['field_12'] ) : '',
			'field_13'		=> isset( $_POST['field_13'] ) ? htmlspecialchars( $_POST['field_13'] ) : '',
			'field_14'		=> isset( $_POST['field_14'] ) ? htmlspecialchars( $_POST['field_14'] ) : '',
			'field_15'		=> isset( $_POST['field_15'] ) ? htmlspecialchars( $_POST['field_15'] ) : ''
		);
		
		$id = isset( $_POST['hid'] ) ? (int)$_POST['hid'] : ''; 
		
		$this->db->where('id', $id );	
		$this->db->where('id_lang', get_admin_lang() );
		
		$this->db->update( $this->table, $data);		
	}
	
	function del_product( $id) {
		$this->db->where('id', $id );
		$this->db->where('id_lang', get_admin_lang() );
		$this->db->delete( $this->table );
	}	
	
	function update_produc_status( $total ) {
	
		for( $k = 1; $k < $total ; $k++ ) {
			
			$status = @(int)$_POST['status_'.$k];
			$order 	= @(int)$_POST['order_'.$k];
			$home 	= @(int)$_POST['home_'.$k];
			$new 	= @(int)$_POST['new_'.$k];
			$focus 	= @(int)$_POST['focus_'.$k];
			
			$id		= @(int)$_POST['id_'.$k]; 
			
			$data = array(
				'status'	=> $status,
				'new'		=> $new,
				'focus'		=> $focus,
				'home'		=> $home,
				'orders'	=> $order,
			);
			
			$this->db->where('id', $id );	
			$this->db->where('id_lang', get_admin_lang() );			
			$this->db->update( $this->table, $data);
		}
	}
}
?>